#!/usr/bin/env python3
# -*- coding:utf-8 -*- 

from pymongo import MongoClient
import time,datetime
import urllib
import re
import sys,getopt
from itertools import groupby
# from operator import itemgetter
import gc
import datetime

try:
    options,args = getopt.getopt(sys.argv[1:],"hm:d:p:",["help","mode=","day=","platform="])
except getopt.GetoptError,e:
    print e
    sys.exit()

MODE=None
DAY=None
PLA=None

#reload(sys)  
#sys.setdefaultencoding('utf8')


#db = MongoClient("mongodb://gysl002:%s@119.254.211.141:27017/test?authMechanism=SCRAM-SHA-1"%(urllib.quote_plus('gyslasdf')))
#db = MongoClient("mongodb://fkcms:%s@121.196.204.57:27017/fkcms?authMechanism=SCRAM-SHA-1"%(urllib.quote_plus('1qaz!QAZ')))
PLATFORM = [
    'chushoutv', 'douyu', 'huya', 'longzhu', 'panda', 'quanmintv', 'zhanqi',
    'yy', 'fanxing', 'kkxiu', 'laifeng', 'meme', 'liujianfang', 'au',
    'yingke', 'yizhibo', 'huajiao', 'xiandanjia', 'zhangyutv', 'qiezhibo'
]

db =None
fkcms=None

'''
插入Mongodb 每日以人为单位的数据
'''
def daily(users=None):
    daily=fkcms.daily
    return daily.insert_many(users)
'''
根据数组得到用户的运算结果
'''
def getResult(fw=None,platform=None):
    _resultList=[]
    # print(i)
    for k, g in fw:
        g=list(g)
        _result={}
        _result['username']=g[-1]['userName']
        _result['avatar']=g[-1]['avatar'] if g[-1].has_key('avatar') else ''
        _result['cateName']=g[-1]['cateName'] if g[-1].has_key('cateName') else ''
        _result['sex']=g[-1]['sex'] if g[-1].has_key('sex') else 0
        _result['count']=len(g)

        _maped=[x['fans'] for x in g]  
        # _result['fans_total']=reduce(lambda x,y:x+y,_maped)
        # _result['fans_min']=reduce(lambda x,y:x if x<y else y,_maped)
        _result['fans_max']=reduce(lambda x,y:y if x<y else x,_maped)
        # _result['fans_avg']=_result['fans_total']/len(g)

        _maped=[x['gift'] for x in g]  
        # _result['gift_total']=reduce(lambda x,y:x+y,_maped)
        # _result['gift_min']=reduce(lambda x,y:x if x<y else y,_maped)
        _result['gift_max']=reduce(lambda x,y:y if x<y else x,_maped)
        # _result['gift_avg']=_result['gift_total']/len(g)

        _maped=[x['onlineCount'] for x in g]  
        # _result['onlineCount_total']=reduce(lambda x,y:x+y,_maped)
        # _result['onlineCount_min']=reduce(lambda x,y:x if x<y else y,_maped)
        _result['onlineCount_max']=reduce(lambda x,y:y if x<y else x,_maped)
        _result['gt10000onlineCount']=len(filter(lambda x: x>=10000, _maped))
        # _result['onlineCount_avg']=_result['onlineCount_total']/len(g)
        # _result['onlineCount_mid']=(_maped[len(_maped)//2] + _maped[~(len(_maped)//2)]) / 2
        _result['platform'] = k.split('-')[0]
        _result['userId'] = k.split('-')[1]
        _result['time'] = DAY
        _resultList.append(_result)
    print('平台%s,收理数%d'%(platform,len(_resultList)))
    return _resultList



'''
从MongoDB中拉取原始数据
'''
def getOriginDate(platforms):
    dict1=[]
    result=[]
    try:
        query={'platform':platforms}
        if DAY is not None:
            query['time']=re.compile(r'%s'%(DAY))
        cursor=fkcms.onlineStar.find(filter=query,projection={'_id': False,'roomUrl':False}).batch_size(1000)
        print('查询%s数'%cursor.count())
        while cursor.alive:
            for doc in cursor:
                dict1.append(doc);
                time.sleep(0.00002)
        #先排序再分组
        dict1.sort(key=lambda x:"%s-%s"% (x['platform'],x['userId']))
        fw=groupby(dict1, lambda x:"%s-%s"% (x['platform'],x['userId']))
        result=getResult(fw,platforms)
        daily(result)
        del dict1
        del result
    except Exception,e:
        print e
    finally:
        cursor.close()
        del cursor
        

'''
入口函数
'''
def init():
    t1 = time.time()
    cleanData()
    for p in  PLA:
        getOriginDate(p)
    t2 = time.time()
    print '共用时%d秒'%(t2-t1)


'''
清理无用数据
'''
def cleanData():
    daily=fkcms.daily
    daily.delete_many({'time':DAY,'platform':{'$in':PLA}})

'''
接受命令行参数
'''
for name,value in options:
    if name in ("-h","--help"):
        usage()
    if name in ("-d","--day"):
        DAY=value
    if name in ("-p","--platform"):
        PLA=value
    if name in ("-m","--mode"):
        MODE=value
if MODE != "prod":
    MODE="dev"

if DAY is None:
    DAY=(datetime.datetime.now()+datetime.timedelta(-1)).strftime("%Y%m%d")
if PLA is not None and PLA.find(',') >0:
    PLA=PLA.split(',')
    PLA=[val for val in PLA if val in PLATFORM]
elif PLA is not None and PLA.find(',') is -1:
    PLA=[PLA]
    PLA=[val for val in PLA if val in PLATFORM]
else:
    PLA=PLATFORM
# cleanData()
print('当前日期参数%s'%(DAY))
print('当前平台参数%s'%(PLA))
print('当前环境参数%s'%(MODE))
if MODE == "dev":
    db= MongoClient("mongodb://127.0.0.1:27017/fkcms?authMechanism=SCRAM-SHA-1")
    fkcms=db.fkcms
else:
    db = MongoClient("mongodb://gysl002:%s@119.254.211.141:27017/test?authMechanism=SCRAM-SHA-1"%(urllib.quote_plus('gyslasdf')))
    fkcms=db.test

init()
    
    



#time.sleep(10)    
